<?php

use App\Http\Admin\Controller\CommonController;
use App\Util\Helper;
use Plugins\AdminBasic\Http\Admin\Controller\SuperConfigController;
use Plugins\AdminBasic\Http\Admin\Service\SuperConfigService;
use Sc\Util\HtmlStructure\Form;
use Sc\Util\HtmlStructure\Form\FormItem;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js\JsCode;
use Sc\Util\HtmlStructure\Html\Js\VueComponents\Temporary;
use Sc\Util\HtmlStructure\Table;

/** @var \Plugins\AdminBasic\Models\BasicSuperConfig $superConfig */



$uploadUrl = Helper::route()->to([CommonController::class, 'upload']);
$froalaUploadUrl = Helper::route()->to([CommonController::class, 'froalaUpload']);

Html::create('配置');

$form  = Form::create('config')->config('label-width', '80px');
$table = Table::create(Helper::route()->to([SuperConfigController::class, 'advancedArrayData'])->url(), 'configList');

$supportForm = array_column(SuperConfigService::supportForm(), 'value');

$table->addColumns(Table\Column::selection());
$form->addFormItems(
    FormItem::hidden('id', 'ID')->default($superConfig->id),
    FormItem::hidden('cid', 'ID'),
);

foreach ($superConfig->config_info as $value) {
    $formType = $value['formType'];
    if (empty($formType) || !in_array($formType, $supportForm)) {
        continue;
    }
    $column = Table\Column::normal($value['title'], $value['field']);
    if ($formType === 'image' || $formType === 'images') {
        $formType = 'upload';
        $column->showImage();
    }
    $formType = match ($formType) {
        'image', 'images', 'uploads' => 'upload',
        'selects'                    => 'select',
        default                      => $formType,
    };

    $formItem = FormItem::{$formType}($value['field'], $value['title']);

    if ($value['extra'] && method_exists($formItem, 'options')) {
        $options = \Plugins\AdminBasic\Http\SuperConfig::getSelectOptions($value['extra']);
        if (count($value['extra']) == 1) {
            $column->setFormat("{{ {$value['field']}_des }}");
            $optionsVar = "System" . substr(md5($value['extra'][0]), 0, 10);
        }else{
            $column->setFormat("{{ {$value['field']}_des }}");
            $optionsVar = $value['field'] . '_options';
        }

        $formItem->col(24)->setOptionsVarName($optionsVar)->options($options);
    }

    if ($formItem instanceof Form\FormItemUpload) {
        $formItem->setUploadUrl($uploadUrl);
    }

    if ($value['formType'] === 'image') {
        $formItem->toImage();
    }elseif ($value['formType'] === 'images'){
        $formItem->toImage(true);
    }elseif ($value['formType'] === 'selects' || $value['formType'] === 'uploads'){
        $formItem->multiple();
    }

    $form->addFormItems($formItem);

    if ($formItem instanceof Form\FormItemEditor) {
        $formItem->setUploadUrl($froalaUploadUrl);
        $formItem->default("");
        continue;
    }
    if ($formItem instanceof Form\FormItemUpload && $value['formType'] !== 'image') {
        continue;
    }
    if ($formItem instanceof Form\FormItemTextarea) {
        continue;
    }

    $table->addColumns($column->addSearch());
}

$form->addFormItems(
    FormItem::submit()->submitUrl(
        Helper::route()->to([SuperConfigController::class, 'updateBaseConfig'])
    )->successClose('current')
        ->success('VueApp.configListGetData()')
);

$table->setHeaderEvent("@primary.Refresh.刷新数据", "this.configListGetData()");


$comment = Temporary::create("xc")->setContent($form->render());

$table->setHeaderEvent('@primary.Plus.新增',
    Table\EventHandler::window('新增')->setComponent($comment)
);
$table->setHeaderEvent('@danger.delete.删除',
    Table\EventHandler::post(
        url: Helper::route()->to([SuperConfigController::class, 'advancedArrayDataDestroy']),
        data: ['ids' => '@selection.map(v => v.cid)', 'id' => $superConfig->id],
        successHandler: JsCode::create("this.\$message.success('删除成功')")->then("this.configListGetData()")
    )->confirmMessage('确认删除选中数据吗？')
        ->addLoading()
);

$table->setRowEvent('@primary.edit.修改',
    Table\EventHandler::window('修改')->setComponent($comment)
);


return $table->render();


